import java.util.Arrays;
import java.util.Comparator;

/**
 * Created by zhangjinrui on 17/6/27.
 */

public class Solution435 {

    public static void main(String args[]) {
        Solution435 s = new Solution435();
    }

    public int eraseOverlapIntervals(Interval[] intervals) {
        Arrays.sort(intervals, new Comparator<Interval>() {
            @Override
            public int compare(Interval o1, Interval o2) {
                if(o1.end == o2.end){
                    return o1.start - o2.start;
                }
                return o1.end - o2.end;
            }
        });

        int pre = 0;
        int cnt = 0;
        for(int i = 1; i < intervals.length ; i ++){
            Interval cur = intervals[i];
            if(cur.start < intervals[pre].end){
                intervals[i] = null;
                cnt ++;
            }else{
                pre = i;
            }
        }
        return cnt;
    }
}
